﻿using Basic.FlowApplication.Config;
using Basic.FlowApplication.Interface;
using Basic.FlowApplication.Model;
using Basic.FlowApplication.Model.AppMsg;
using Basic.FlowExtendService;
using Basic.FlowRemoteModel;
using Basic.FormRemoteModel.QueryInter.Model;
using Basic.HrRemoteModel;
using Basic.HrRemoteModel.Msg.Model;
using WeDonekRpc.Helper;

namespace Basic.FlowApplication.Extend
{
    internal class BeginMsgService : IBeginMsgService
    {

        private readonly IMsgService _Msg;
        private readonly IFlowConfig _Config;
        private readonly IFormInterService _Form;

        public BeginMsgService ( IMsgService msg, IFlowConfig config, IFormInterService form )
        {
            this._Msg = msg;
            this._Config = config;
            this._Form = form;
        }

        public void HandlerMsg ( AuditBeginBody body )
        {
            FlowNode node = body.Flow.Nodes[body.Flow.BeginNode];
            MsgAddItem add = new MsgAddItem
            {
                EmpId = body.User.EmpId,
                FormEmpId = body.User.EmpId,
                LinkPk = body.ItemId,
                MsgTitle = body.DocTitle,
                Priority = HrMsgPriority.普通,
                Tag = this._Config.StaySubmitAuditTag,
                Body = this._GetBody(body, node),
                Attrs = new Dictionary<string, object>
                {
                    {"NodeName", node.NodeName },
                    {"AuditType", (int)node.AuditType },
                    {"AppName", body.App.AppName }
                 }
            };
            MsgAdd msg = new MsgAdd
            {
                AppId = body.App.Id,
                LinkType = MsgConstDic.TodoLinkType,
                MsgType = HrMsgType.待办消息,
                MsgClassify = MsgConstDic.MyTodoMsgClassify,
                Items = new MsgAddItem[] { add },
            };
            ToVoidItem toVoid = null;
            if ( body.ToVoidLogId.HasValue )
            {
                toVoid = new ToVoidItem
                {
                    LinkPk = [body.ToVoidItemId.Value],
                    LinkType = MsgConstDic.TodoLinkType,
                    Tag = this._Config.StaySubmitAuditTag
                };
            }
            this._Msg.Send(msg, toVoid);
        }
        private Dictionary<long, FormVal> _GetFormBody ( AuditBeginBody body, long formId, FlowAppMsgTemplate[] form )
        {
            if ( !form.IsNull() )
            {
                return this._Form.GetFormValues(new FormValsGetArg
                {
                    FormId = formId,
                    LinkPK = body.LinkPk,
                    LinkType = body.LinkType,
                    Column = form.ConvertAll(a => new KeyValuePair<long, string>(a.ColId.Value, a.Keywords))
                });
            }
            return null;
        }
        private MsgBody[] _GetBody ( AuditBeginBody data, FlowNode node )
        {
            if ( data.App.Template.IsNull() )
            {
                return null;
            }
            long formId = node.FormId.GetValueOrDefault(data.Flow.DefFormId.Value);
            FlowAppMsgTemplate[] form = data.App.Template.FindAll(a => a.DataType == FlowAppMsgDataType.表单 && a.FormId == formId);
            Dictionary<long, FormVal> vals = this._GetFormBody(data, formId, form);
            return form.ConvertAll(a =>
            {
                MsgBody body = new MsgBody
                {
                    Label = a.Label,
                    IsRed = a.IsRed,
                    IsRow = a.IsRow
                };
                if ( a.DataType == FlowAppMsgDataType.自定义 )
                {
                    body.Value = a.Value;
                }
                else if ( a.DataType == FlowAppMsgDataType.表单 )
                {
                    if ( vals != null && vals.TryGetValue(a.ColId.Value, out FormVal val) )
                    {
                        body.Value = val.FormatString();
                    }
                }
                else if ( a.DataType == FlowAppMsgDataType.审批字段 )
                {
                    if ( a.Value == "AuditType" )
                    {
                        body.Value = node.AuditType.ToString();
                    }
                    else if ( a.Value == "NodeName" )
                    {
                        body.Value = node.NodeName;
                    }
                    else if ( a.Value == "AppName" )
                    {
                        body.Value = data.App.AppName;
                    }
                }
                return body;
            });
        }
    }
}
